package com.example.kechengsheji;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import java.util.ArrayList;

public class study extends Activity {
    private EditText rshangpin,bnum;
    private Button next,total,delete,sum,jiesuan;
    String name1;
    private TextView sshangpin,smoney,snum,ssum,stotal;
    final DatabaseHelper dbHelper=new DatabaseHelper(study.this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.study);
        next = findViewById(R.id.next);
        total=findViewById(R.id.total);
        rshangpin = findViewById(R.id.rshangpin);
        bnum = findViewById(R.id.bnum);
        ssum=findViewById(R.id.ssum);
        sshangpin = findViewById(R.id.sshangpin);
        smoney = findViewById(R.id.smoney);
        snum=findViewById(R.id.snum);
        stotal=findViewById(R.id.stotal);

        ArrayList<Integer> myNumbers = new ArrayList<Integer>();

        next.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name1 = rshangpin.getText().toString();
//                shownum.setText(null);
//                showmoney.setText(null);
                if (name1.equals("")) {
                    Cursor cursor = db.rawQuery("select * from information", null);
                } else {
                    Cursor cursor = db.rawQuery("select * from information where bshangpin=?", new String[]{name1});
                    while (cursor.moveToNext()) {
                        String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
                        int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
//                        tv_showshangpin.setText(null);
                        sshangpin.setText(sshangpin.getText() + "\n" + newshangpin);
//                        tv_showmoney.setText(null);
                        smoney.setText(smoney.getText() + "\n" + newmoney);
                        //插入数量
                        String newnum = bnum.getText().toString();
//                        tv_shownum.setText(null);
                        snum.setText(snum.getText() + "\n" + newnum);

                        int i = Integer.valueOf(newnum).intValue();//商品数量
                        int heji = newmoney * i;//单个商品的合计
//                        tv_sum.setText(null);
                        ssum.setText(ssum.getText() + "\n" + heji);

                        myNumbers.add(heji);


                    }
                }


            }


        });


        total.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer sum = 0;
                for (Integer tmp : myNumbers) {
                    sum = tmp + sum;
                }
                stotal.setText(null);
                stotal.setText(stotal.getText()+"\t"+sum);

            }
        });


    }


    public void myShow(){
        final DatabaseHelper dbHelper=new DatabaseHelper(study.this);
        SQLiteDatabase db=dbHelper.getReadableDatabase();


        sshangpin.setText("商品");
        smoney.setText("单价");

        Cursor cursor = db.rawQuery("select * from information",null);

        while (cursor.moveToNext()) {
            String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
            int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
            sshangpin.setText(sshangpin.getText() + "\n" + newshangpin);
            smoney.setText(smoney.getText()+"\n" + newmoney);
        }
        cursor.close();
        db.close();
    }

}







